<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\sredirect;

use builder\helper\Url;

/**
 * Page类型
 * @author cleverstone
 * @since ym2.0
 */
class Page extends SrType
{
    const TYPE = 'page';

    /**
     * @var array
     */
    private $_options = [
        'route' => '',
        'params' => [],
        'target' => '_self',
        //支持有效的JS窗口对象
        'window' => 'self',
    ];

    /**
     * @param string|array $params 请求参数
     * one:
     * ```php
     * // 指定成功返回中的字段
     * $this->params(['id']);
     *
     * ```
     *
     * two:
     * ```php
     * // key-value, 字段指定值
     * $this->params(['id' => 1]);
     *
     * ```
     *
     * three:
     * ```php
     * // key-占位符, 字段+字段占位符
     * $this->params(['action' => ':status']);
     *
     * ```
     * @return $this
     */
    public function params($params)
    {
        $this->_options['params'] = (array)$params;
        return $this;
    }

    /**
     * @param string $route 路由或外链
     * ```php
     * 可以直接指定路由
     * $this->route('user/edit');
     *
     * 也可以指定外部链接
     * $this->route('https://ym2.cleverstone.top');
     *
     * ```
     *
     * @return $this
     */
    public function route($route)
    {
        $this->_options['route'] = Url::to($route, '');
        return $this;
    }

    /**
     * 设置跳转目标
     * @param string $target
     * @return $this
     */
    public function target($target)
    {
        $this->_options['target'] = (string)$target;
        return $this;
    }

    /**
     * 设置有效JS窗口对象
     * - self
     * - window
     * - global
     * - parent
     * - parent.parent
     * - parent.parent....
     * - top
     * @param string $window
     * @return $this
     */
    public function window($window)
    {
        $this->_options['window'] = (string)$window;
        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        $options = $this->_options;
        $options['type'] = self::TYPE;

        return $options;
    }
}